package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {
    //初始化mapper对象
    private PermissionMapper permissionMapper;

    @Autowired
    public void setPermissionMapper(PermissionMapper permissionMapper) {
        this.permissionMapper = permissionMapper;
    }

    @Transactional
    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Transactional
    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    @Transactional
    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    //分页查询
    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Permission> permissions = permissionMapper.selectForList(qo);
        return new PageInfo<>(permissions);
    }

    @Autowired
    private RequestMappingHandlerMapping handlerMapping;
    @Override
    public void reload() {
        //查询所有权限
        List<Permission> permissions = permissionMapper.selectAll();
        //通过handlerMapping来获取到所有的请求方法分装成map集合
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
        //获取到map集合中的value
        Collection<HandlerMethod> values = handlerMethods.values();
        for (HandlerMethod m : values) {
            RequiredPermission methodAnnotation = m.getMethodAnnotation(RequiredPermission.class);
            //判断是否有注解
            if (methodAnnotation == null) {
                continue;
            }
            //获取到注解中的name和expermission
            String name = methodAnnotation.name();
            String expression = methodAnnotation.expression();
            //通过name去mappeer里找找是否是已经存在这个权限
            Permission permission = permissionMapper.selectBypermission(name,expression);
            //如果在数据库没找到,就插入新的数据
            if (permission == null) {
                Permission p = new Permission();
                p.setName(name);
                p.setExpression(expression);
                permissionMapper.insert(p);
            }
        }
    }

    @Override
    public List<Permission> selectByRid(Long id) {
        return permissionMapper.selectByRid(id);
    }


}
